// pages/emgResponse/emgResponse.js
var app = getApp()
var util = require('../../../utils/util.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        emgResponseList: [],//处于应急响应的列表 status=3
        emgSummary: [],//处于应急总结的列表 status=11

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.loadEmgResponse(3)
        this.loadEmgResponse(11)
    },
    /**
     * 加载应急响应
     */
    loadEmgResponse(status) {
        var that = this
        var header;
        header = {
            'Cookie': wx.getStorageSync("sessionid"), //读取cookie
            "Content-Type": "application/x-www-form-urlencoded"
        };
        var params = {
            history: 0,//2是隐患排查
            status: status //状态是已经结束的
        }
        wx.request({
            header: header,
            url: app.globalData.baseUrl + '/safe/planPublic/findMotherCompanyEmg',
            method: "GET",
            data: params,
            success(res) {
                if (res.data.success) {
                    if (status == 3) {//应急响应的状态
                        that.setData({
                            emgResponseList: res.data.data.list
                        })
                    } else if (status == 11) {
                        that.setData({//应急总结的状态
                            emgSummary: res.data.data.list
                        })
                    }
                }
            }
        })
    },
    /**
     * 响应按钮，响应应急
     * @param e
     */
    response(e) {
        var startInfo = e.currentTarget.dataset.startinfo
        var title = e.currentTarget.dataset.title
        var responseId=e.currentTarget.dataset.resid
        var resIndex=e.currentTarget.dataset.index
        var that=this
        wx.showModal({
            title: title,
            content: startInfo,
            confirmText: "响应",
            success(res) {
                if (res.confirm) {
                    console.log('用户点击确定')
                    wx.showLoading({
                        title: '保存中.....',
                    })
                    var header;
                    header = {
                        'Cookie': wx.getStorageSync("sessionid"), //读取cookie
                        "Content-Type": "application/x-www-form-urlencoded"

                    };
                    var params = {
                        id: responseId,
                        responseTime: new Date(),
                        response:true
                    }
                    wx.request({
                        header: header,
                        url: app.globalData.baseUrl + '/safe/planPublicResponse/update',
                        method: "POST",
                        data: params,
                        success(res) {
                            wx.hideLoading()
                            if (res.data.success) {
                                var responseList=that.data.emgResponseList
                                responseList[resIndex].myResponse=res.data.data
                                that.setData({
                                    emgResponseList:responseList
                                })
                            }
                        }
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    /**
     * 返回首页
     */
    backIndex(){
        wx.redirectTo({
            url:'/pages/index/index'
        })
    },
    /**
     * 跳转到响应的详情页
     */
    emgResponseDetail(e) {
        var index = e.currentTarget.dataset.index
        wx.redirectTo({
            url: '/pages/emergency/emgResponseDetail/emgResponseDetail?emgResponse=' + JSON.stringify(this.data.emgResponseList[index])
        })
    },
    /**
     * 标签页
     * @param e
     */
    tabOnChange(e) {

    },
    //应急响应的点击事件
    summaryClick:function (e) {
        let typeLevelStr=e.currentTarget.dataset.type + e.currentTarget.dataset.level;

        wx.navigateTo({
            url: '/pages/emgResponse/editSummary/editSummary?emergencyId=' + e.currentTarget.dataset.emergencyId + '&startId=' + e.currentTarget.dataset.startId + '&name=' + e.currentTarget.dataset.name + '&typeLevel=' + typeLevelStr
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})